package com.martin.fast_frame_compose.page.refresh

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.martin.lib_base.interfaces.IItemType
import com.martin.lib_base.ui.screen.Center
import com.martin.lib_base.ui.screen.CommonRefreshBox
import com.martin.lib_base.ui.screen.CommonRefreshColumn
import com.martin.lib_base.ui.screen.CommonRefreshVerticalGrid
import com.martin.lib_base.ui.screen.TitleBar
import com.martin.lib_base.ui.theme.ColorPrimary
import com.martin.lib_base.ui.theme.CommonTheme

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RefreshPage(vModel: RefreshViewModel) {
    CommonTheme(lifecycleObserver = vModel) {
        HorizontalPager(state = rememberPagerState { 3 }) {
            when (it) {
                0 -> ColumnRefresh(vModel)
                1 -> GridRefresh(vModel)
                2 -> BoxRefresh(vModel)
            }
        }
    }
}

@Composable
private fun ColumnRefresh(vModel: RefreshViewModel) {
    Column(modifier = Modifier.fillMaxSize()) {
        TitleBar(title = "ColumnRefresh", onBackClick = { vModel.navigateUp() })
        CommonRefreshColumn(
            pageModel = vModel.pageModel,
            typeItemMap = mapOf(IItemType.ITEM_TYPE_COMMON to {
                Item(it)
            })
        )
    }
}

@Composable
private fun GridRefresh(vModel: RefreshViewModel) {
    Column(modifier = Modifier.fillMaxSize()) {
        TitleBar(title = "GridRefresh", onBackClick = { vModel.navigateUp() })
        CommonRefreshVerticalGrid(
            pageModel = vModel.pageModel,
            typeItemMap = mapOf(IItemType.ITEM_TYPE_COMMON to {
                Item(it)
            }),
            columns = GridCells.Fixed(2)
        )
    }
}

@Composable
private fun BoxRefresh(vModel: RefreshViewModel) {
    Column(modifier = Modifier.fillMaxSize()) {
        TitleBar(title = "BoxRefresh", onBackClick = { vModel.navigateUp() })
        CommonRefreshBox(
            refreshing = vModel.boxRefreshing.value, onRefresh = { vModel.onBoxRefresh() },
            modifier = Modifier.fillMaxSize()
        ) {
            Center(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
            ) {
                Text(text = "没有实际内容")
            }
        }
    }
}

@Composable
private fun Item(data: Any) {
    Center(
        modifier = Modifier
            .padding(10.dp)
            .fillMaxWidth()
            .height(80.dp)
            .background(ColorPrimary)
    ) {
        Text(text = data.toString())
    }
}